<?php
require_once("database.php");
class xl_bai_viet extends database
{
	function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from bai_viet limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_theo_loai($ma_loai, $vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from bai_viet where ma_loai_bai_viet = $ma_loai limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function so_luong($ma_loai)
	{
		$sql = "select count(*) from bai_viet where ma_loai_bai_viet = '$ma_loai' ";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function xem($ma)
	{
		$ma = intval($ma);

		$sql = "select * from bai_viet where ma = '$ma'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xoa($ma)
	{
		$ma = intval($ma);

		$sql = "delete from bai_viet where ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function them($ma_loai_bai_viet, $ma_nguoi_dung, $tieu_de, $noi_dung_tom_tat, $noi_dung_chi_tiet, $ngay_tao, $so_lan_xem, $trang_thai, $hinh)
	{
		$ma_loai_bai_viet = intval($ma_loai_bai_viet);
		$ma_nguoi_dung = intval($ma_nguoi_dung);
		$tieu_de = stripslashes($tieu_de);
		$tieu_de = addslashes($tieu_de);
		$noi_dung_tom_tat = stripslashes($noi_dung_tom_tat);
		$noi_dung_tom_tat = addslashes($noi_dung_tom_tat);
		$noi_dung_chi_tiet = stripslashes($noi_dung_chi_tiet);
		$noi_dung_chi_tiet = addslashes($noi_dung_chi_tiet);
		$so_lan_xem = intval($so_lan_xem);
		$hinh = stripslashes($hinh);
		$hinh = addslashes($hinh);

		$sql = "INSERT INTO bai_viet(ma_loai_bai_viet,ma_nguoi_dung,tieu_de,noi_dung_tom_tat,noi_dung_chi_tiet,ngay_tao,so_lan_xem,trang_thai,hinh) VALUES ('$ma_loai_bai_viet','$ma_nguoi_dung','$tieu_de','$noi_dung_tom_tat','$noi_dung_chi_tiet','$ngay_tao','$so_lan_xem','$trang_thai','$hinh')";
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat($ma, $ma_loai_bai_viet, $ma_nguoi_dung, $tieu_de, $noi_dung_tom_tat, $noi_dung_chi_tiet, $ngay_tao, $so_lan_xem, $trang_thai, $hinh)
	{
		$ma = intval($ma);
		$ma_loai_bai_viet = intval($ma_loai_bai_viet);
		$ma_nguoi_dung = intval($ma_nguoi_dung);
		$tieu_de = stripslashes($tieu_de);
		$tieu_de = addslashes($tieu_de);
		$noi_dung_tom_tat = stripslashes($noi_dung_tom_tat);
		$noi_dung_tom_tat = addslashes($noi_dung_tom_tat);
		$noi_dung_chi_tiet = stripslashes($noi_dung_chi_tiet);
		$noi_dung_chi_tiet = addslashes($noi_dung_chi_tiet);
		$so_lan_xem = intval($so_lan_xem);
		$hinh = stripslashes($hinh);
		$hinh = addslashes($hinh);

		$sql ="UPDATE bai_viet SET ma_loai_bai_viet= '$ma_loai_bai_viet', ma_nguoi_dung= '$ma_nguoi_dung', tieu_de= '$tieu_de', noi_dung_tom_tat= '$noi_dung_tom_tat', noi_dung_chi_tiet= '$noi_dung_chi_tiet', ngay_tao= '$ngay_tao', so_lan_xem= '$so_lan_xem', trang_thai= '$trang_thai', hinh= '$hinh' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_trang_thai($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE bai_viet SET trang_thai = 1 - trang_thai WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function tim_kiem ($tu_khoa)
	{
		$sql = "select * from bai_viet where	$tieu_de like %tu_khoa% or 	$noi_dung_tom_tat like %tu_khoa% or 	$noi_dung_chi_tiet like %tu_khoa% or 	$hinh like %tu_khoa%"; 
		$this->setQuery($sql);
		return $this->loadAllRow();	
	}
 }
?>